import {
  Controller,
  Post,
  Inject,
  UseInterceptors,
  UploadedFile,
  Body,
  UseGuards,
} from '@nestjs/common';
import { UploadService } from './upload.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { storage } from './file.storage';
import { FileSizeValidationPipe } from './FileSizeValidationPipe';
import {
  ApiBearerAuth,
  ApiBody,
  ApiConsumes,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { BasicVo } from 'src/pojo/vo/basic.vo';
import { JwtGuard } from 'src/guards/jwt.guard';
import { ConfigService } from '@nestjs/config';
import { ConfigEnum } from 'src/enum/config.enum';

@ApiTags('上传模块')
@Controller('upload')
export class UploadController {
  @Inject()
  private readonly uploadService: UploadService;

  @Inject()
  private readonly configService: ConfigService;

  @ApiBearerAuth()
  @ApiOperation({ summary: '文件上传' })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
          description: '文件必传限制50M',
        },
      },
    },
  })
  @ApiResponse({
    status: 200,
    description: '通用数据',
    type: BasicVo,
  })
  @UseGuards(JwtGuard)
  @Post()
  @UseInterceptors(
    FileInterceptor('file', {
      storage: storage,
    }),
  )
  uploadFile(
    @UploadedFile(FileSizeValidationPipe) file: Express.Multer.File,
    @Body() body,
  ) {
    console.log('body', body);
    console.log('file', file);
    return {
      originalName: file.originalname,
      name: file.filename,
      url: `${this.configService.get(ConfigEnum.FILE_PATH)}/${file.filename}`,
    };
  }
}
